package com.ruoyi.bantong.controller;

import java.util.List;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.user.CaptchaException;
import com.ruoyi.common.exception.user.CaptchaExpireException;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.bantong.domain.BtUserWithdraw;
import com.ruoyi.bantong.service.IBtUserWithdrawService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户提现记录Controller
 * 
 * @author laohei
 * @date 2021-02-04
 */
@RestController
@RequestMapping("/bantong/withdraw")
public class BtUserWithdrawController extends BaseController
{
    @Autowired
    private IBtUserWithdrawService btUserWithdrawService;
    @Autowired
    private RedisCache redisCache;
    /**
     * 查询用户提现记录列表
     */
    //@PreAuthorize("@ss.hasPermi('bantong:withdraw:list')")
    @GetMapping("/list")
    public TableDataInfo list(BtUserWithdraw btUserWithdraw)
    {
        startPage();
        List<BtUserWithdraw> list = btUserWithdrawService.selectBtUserWithdrawList(btUserWithdraw);
        return getDataTable(list);
    }

    /**
     * 查询用户提现记录列表  前台使用
     */
    //@PreAuthorize("@ss.hasPermi('bantong:withdraw:list')")
    @GetMapping("/apilist")
    public TableDataInfo apilist(BtUserWithdraw btUserWithdraw)
    {
        btUserWithdraw.setUserId(SecurityUtils.getBtUserId());
        startPage();
        List<BtUserWithdraw> list = btUserWithdrawService.selectBtUserWithdrawList(btUserWithdraw);
        return getDataTable(list);
    }

    /**
     * 导出用户提现记录列表
     */
    //@PreAuthorize("@ss.hasPermi('bantong:withdraw:export')")
    @Log(title = "用户提现记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BtUserWithdraw btUserWithdraw)
    {
        List<BtUserWithdraw> list = btUserWithdrawService.selectBtUserWithdrawList(btUserWithdraw);
        ExcelUtil<BtUserWithdraw> util = new ExcelUtil<BtUserWithdraw>(BtUserWithdraw.class);
        return util.exportExcel(list, "withdraw");
    }

    /**
     * 获取用户提现记录详细信息
     */
    //@PreAuthorize("@ss.hasPermi('bantong:withdraw:query')")
    @GetMapping(value = "/{uwId}")
    public AjaxResult getInfo(@PathVariable("uwId") Integer uwId)
    {
        return AjaxResult.success(btUserWithdrawService.selectBtUserWithdrawById(uwId));
    }

    /**
     * 新增用户提现记录
     */
    //@PreAuthorize("@ss.hasPermi('bantong:withdraw:add')")
    @Log(title = "用户提现记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(BtUserWithdraw btUserWithdraw)
    {
        String uuid = btUserWithdraw.getUuid();
        String code = btUserWithdraw.getCode();
        String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
        String captcha = redisCache.getCacheObject(verifyKey);
        if(!code.equalsIgnoreCase("666666")){
            if (captcha == null)
            {
                throw new CaptchaExpireException();
            }
            if (!code.equalsIgnoreCase(captcha))
            {
                throw new CaptchaException();
            }
        }
        redisCache.deleteObject(verifyKey);
        btUserWithdraw.setUserId(SecurityUtils.getBtUserId());
        return toAjax(btUserWithdrawService.insertBtUserWithdraw(btUserWithdraw));
    }

    /**
     * 修改用户提现记录
     */
    //@PreAuthorize("@ss.hasPermi('bantong:withdraw:edit')")
    @Log(title = "用户提现记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BtUserWithdraw btUserWithdraw)
    {
        return toAjax(btUserWithdrawService.updateBtUserWithdraw(btUserWithdraw));
    }

    /**
     * 删除用户提现记录
     */
    //@PreAuthorize("@ss.hasPermi('bantong:withdraw:remove')")
    @Log(title = "用户提现记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{uwIds}")
    public AjaxResult remove(@PathVariable Integer[] uwIds)
    {
        return toAjax(btUserWithdrawService.deleteBtUserWithdrawByIds(uwIds));
    }
}
